package com.jeasonzhao.report.model;

import com.jeasonzhao.commons.basic.StringPairCollection;
import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.commons.utils.DataTypes;
import com.jeasonzhao.model.AbstractModelItem;
import com.jeasonzhao.model.Model;
import com.jeasonzhao.model.ModelField;
import com.jeasonzhao.commons.json.JSONClass;
import com.jeasonzhao.commons.json.JSONException;
import com.jeasonzhao.commons.utils.ConvertEx;

@Model("param")
public class ReportParam extends AbstractModelItem
{
    private static final long serialVersionUID = 1L;
    @ModelField(names = "reportId")
    private String m_strReportId = null;

    @ModelField(names = "sortNumber")
    private int m_nSortNumber = -1;

    @ModelField(names = "param,parameter")
    private String m_strParamName = null;

    @ModelField(names = "title,name")
    private String m_strTitle = null;

    @ModelField(names = "dictId,dict")
    private String m_strDictId = null;

    @ModelField(names = "dataType")
    private DataTypes m_dataType = null;

    @ModelField(names = "replaceValue")
    private boolean m_isReplaceValue = false;

    @ModelField(names = "singleSelect")
    private boolean m_isSingleSelect = true;

    @ModelField(names = "defaultValue")
    private String m_strDefaultValueScript = null;

    @ModelField(names = "notes,comment,comments")
    private String m_strNotes = null;

    @ModelField(names = "isFixed,fixed")
    private boolean m_isFixedValue = false;

    //JSON Style {attribute:value;}
    @ModelField(names = "selector,selectorMode")
    private String m_strSelectorMode = null;

    @ModelField(names = "optional")
    private boolean m_isOptional = false;

    ///////Dyn Parameters
    private ObjectCollection m_values = null;
    public ReportParam()
    {
        super();
    }

    public ReportParam(ReportParam param)
    {
        if(null == param)
        {
            return;
        }
        this.m_strParamName = param.m_strParamName;
        this.m_strTitle = param.m_strTitle;
        this.m_strDictId = param.m_strDictId;
        this.m_dataType = param.m_dataType;
        this.m_isReplaceValue = param.m_isReplaceValue;
        this.m_isSingleSelect = param.m_isSingleSelect;
        this.m_strDefaultValueScript = param.m_strDefaultValueScript;
        this.m_nSortNumber = param.m_nSortNumber;
        this.m_strNotes = param.m_strNotes;
        this.m_isFixedValue = param.m_isFixedValue;
        this.m_strSelectorMode = param.m_strSelectorMode;
        this.m_isOptional = param.m_isOptional;

        if(null != param.m_values)
        {
            this.m_values = new ObjectCollection();
            for(int n = 0;n < param.m_values.size();n++)
            {
                if(null != param.m_values.get(n))
                {
                    this.m_values.add(param.m_values.get(n));
                }
            }
        }
    }

    public static String getFormatedValueString(Object obj)
    {
        return null == obj ? "" : Algorithms.toString(obj);
    }

    public void addValue(Object obj)
    {
        if(null == obj)
        {
            return;
        }
        if(null == m_values)
        {
            m_values = new ObjectCollection();
        }
        m_values.add(obj);
    }

    public void setValue(Object ...initParameters)
    {
        m_values = new ObjectCollection();
        if(null == initParameters)
        {
            return;
        }
        for(Object item : initParameters)
        {
            if(null == item)
            {
                continue;
            }
            else if(java.util.Iterator.class.isAssignableFrom(item.getClass()))
            {
                for(Object subitem : Algorithms.toArray(item))
                {
                    if(subitem != null)
                    {
                        m_values.add(subitem);
                    }
                }
            }
            else
            {
                m_values.add(item);
            }
        }
    }

    public String getSelectorMode()
    {
        return this.m_strSelectorMode;
    }

    public DataTypes getDataType()
    {
        return m_dataType == null ? DataTypes.STRING : m_dataType;
    }

    public String getDefaultValueScript()
    {
        return m_strDefaultValueScript == null || m_strDefaultValueScript.trim().length() < 1 ? null : m_strDefaultValueScript.trim();
    }

    public String getDictId()
    {
        return m_strDictId == null || m_strDictId.trim().length() < 1 ? null : m_strDictId.trim();
    }

    public boolean isFixedValue()
    {
        return m_isFixedValue;
    }

    public boolean isOptional()
    {
        return m_isOptional;
    }

    public boolean isReplaceValue()
    {
        return m_isReplaceValue;
    }

    public boolean isSingleSelect()
    {
        return m_isSingleSelect;
    }

    public String getNotes()
    {
        return m_strNotes == null || m_strNotes.length() < 1 ? null : m_strNotes;
    }

    public String getParam()
    {
        return m_strParamName;
    }

    public int getSortNum()
    {
        return m_nSortNumber;
    }

    public String getTitle()
    {
        return m_strTitle;
    }

    public void setTitle(String title)
    {
        this.m_strTitle = title;
    }

    public void setSortNum(int sortNum)
    {
        this.m_nSortNumber = sortNum;
    }

    public void setParam(String param)
    {
        this.m_strParamName = param;
    }

    public void setNotes(String notes)
    {
        this.m_strNotes = notes;
    }

    public void setIsReplaceValue(boolean isReplaceValue)
    {
        this.m_isReplaceValue = isReplaceValue;
    }

    public void setIsOptional(boolean isOptional)
    {
        this.m_isOptional = isOptional;
    }

    public void setIsFixedValue(boolean isFixedValue)
    {
        this.m_isFixedValue = isFixedValue;
    }

    public ReportParam setDictId(String dictId)
    {
        this.m_strDictId = dictId;
        return this;
    }

    public ReportParam setDefaultValueScript(String defaultValue)
    {
        this.m_strDefaultValueScript = defaultValue;
        return this;
    }

    public ReportParam setDataType(DataTypes dataType)
    {
        this.m_dataType = dataType;
        return this;
    }

    private JSONClass m_jsonSelectMode = null;
    public void setSelectorMode(String strMode)
    {
        this.m_strSelectorMode = strMode;
        this.m_jsonSelectMode = null;
    }

    public String getSelectorValue(String ...key)
    {
        if(null == key || key.length < 1)
        {
            return null;
        }
        if(null == m_jsonSelectMode)
        {
            try
            {
                m_jsonSelectMode = null == m_strSelectorMode || m_strSelectorMode.trim().length() < 1 ? null : JSONClass.from(m_strSelectorMode);
            }
            catch(JSONException e)
            {
            }
        }
        if(null == m_jsonSelectMode)
        {
            return null;
        }
        else
        {
            return m_jsonSelectMode.valueOf(key);
        }
    }

    public int getSelectorLine()
    {
        if(null == m_jsonSelectMode)
        {
            try
            {
                m_jsonSelectMode = null == m_strSelectorMode || m_strSelectorMode.trim().length() < 1 ? null : JSONClass.from(m_strSelectorMode);
            }
            catch(JSONException e)
            {
            }
        }
        return null == m_jsonSelectMode ? -1 : ConvertEx.toInt(m_jsonSelectMode.valueOf("row","line","rowid","lineid"), -1);
    }

    public void setIsSingleSelect(boolean isSingleSelect)
    {
        this.m_isSingleSelect = isSingleSelect;
    }

    public String toString()
    {
        return this.getTitle() + ":" + this.m_strParamName;
    }

    public boolean hasBeenSetValues()
    {
        return m_values != null && m_values.size() > 0;
    }

    public ObjectCollection getValues()
    {
        return this.m_values;
    }

    public Object getSingleParamValue()
    {
        return this.getDataType().castValue(null == m_values || m_values.size() < 1 ? null : m_values.get(m_values.size() - 1));
    }

    public int valueSize()
    {
        return null == m_values ? 0 : m_values.size();
    }

    public Object getValue(int nx)
    {
        return this.getDataType().castValue(null == m_values ? null : m_values.elementAt(nx));
    }

    public Object getValue()
    {
        return this.getValue(0);
    }

    public StringPairCollection getSelectorFunctionParameters()
    {
        String strSelector = this.getSelectorMode();
        if(null == strSelector || strSelector.trim().length() < 1)
        {
            return new StringPairCollection();
        }
        int n1 = strSelector.indexOf("(");
        int n2 = strSelector.indexOf(")");
        StringPairCollection parameters = new StringPairCollection();
        if(n1 > 0 && n2 > n1)
        {
            strSelector = strSelector.substring(n1 + 1,n2);
            parameters = StringPairCollection.from(strSelector,",",":");
        }
        return parameters;
    }

    public void clearValues()
    {
        m_values = null;
    }

    public String getGuid()
    {
        return this.getParam();
    }

    public String getReportId()
    {
        return this.m_strReportId;
    }

    public void setReportId(String str)
    {
        this.m_strReportId = str;
    }

    public Object getFirstValue()
    {
        return this.getValue(0);
    }
}
